<?php

namespace Common\Model;

use Think\Model;

/** 
 * @author Lain
 * 
 */
class PositionModel extends Model {

    public function getAllPosition(){
        $list = $this->index('posid')->select();
        return $list;
    }

    public function position_update($id, $modelid, $catid, $posid, $data, $expiration = 0, $undel = 0, $model = 'content_model'){

        //删除旧的
        $map['id'] = $id;
        $map['modelid'] = $modelid;
        $map['catid'] = $catid;
        M('position_data')->where($map)->delete();
        if(is_array($posid) && !empty($posid)){
            foreach ($posid as $pid) {
                $info = [];
                $info = $map;
                $info['posid'] = $pid;
                $info['data'] = array2string($data);
                $info['thumb'] = empty($data['thumb']) ? 0 : 1;
                $result = M('position_data')->add($info);
            }
        }
    }

    //获取推荐文章
    public function getList($posid, $num, $params = []){
        if($params['catid']){
            $map['catid'] = $params['catid'];
        }
        $map['posid'] = $posid;
        $pos_arr = M('position_data')->where($map)->limit($num)->select();
        if(empty($pos_arr)){
            return [];
        }
        $array = [];
        foreach ($pos_arr as $info) {
            $key = $info['catid'].'-'.$info['id'];
            $array[$key] = string2array($info['data']);
            // $array[$key]['url'] = go($info['catid'],$info['id']);
            $array[$key]['id'] = $info['id'];
            $array[$key]['catid'] = $info['catid'];
            $array[$key]['listorder'] = $info['listorder'];
        }
        return $array;
    }
}

?>